Page({
    data: {
        clothes: null,
        color: null,
        photo: null,
        size: null,
        heart: true,
        second: false,
        scrollTop: 0,
        bottom: -300,
        imageIndex: 0,
        referShow: false
    },
    onLoad(option) {
        let self = this
        let baseUrl = 'https://xiongmaoyin.com'
        wx.request({
            url: 'https://xiongmaoyin.com/api/one',
            method: 'POST',
            data: {
                id: option.id
            },
            header: {
                Authorization: 'Bearer' + wx.getStorageSync('token')
            },
            success: (res) => {
                if(res.data.code == 200) {
                    let detail = res.data.data
                    let clothes = detail.clothes[0]
                    let size = detail.size[0], _size = []
                    let color = detail.color
                    let photo = detail.photo
                    let heart = detail.favor
                    if(heart == 1) {
                        self.setData({
                            heart: false
                        })
                    }
                    let price = clothes.price
                    clothes.price = Number(clothes.price)
                    if (isNaN(clothes.price) == true) {
                      console.log(detail.clothes[0].price)
                        clothes.price = Number(price.split('-')[0])
                    }
                    for (let i in size) {
                        if (size[i] == 1) {
                            _size.push(i.toUpperCase())
                        }
                    }
                    for (let i in color) {
                        color[i].index = i
                        if (color[i].cover != null) {
                            color[i].cover = baseUrl + color[i].cover
                        } 
                    }
                    if(photo.length > 0) {
                        for (let i in photo) {
                            photo[i] = baseUrl + photo[i]
                        }
                    }
                    else {
                        photo = null
                    }              
                    self.setData({
                        clothes: clothes,
                        color: color,
                        photo: photo,
                        size: _size
                    })
                }
            }
        })
        
    },
    handleColor(e) {
        let index = e.currentTarget.dataset.color.index
        this.setData({
            imageIndex: index
        })
    },
    handleLike() {
      console.log(this.data.clothes.id)
        wx.request({
            url: 'https://xiongmaoyin.com/api/favor',
            method: 'POST',
            data: {
                clothes: this.data.clothes.id
            },
            header: {
                Authorization: 'Bearer' + wx.getStorageSync('token')
            },
            success: (res) => {
                if(res.data.code == 200) {
                    if (this.data.heart == true) {
                        this.setData({
                            heart: false
                        })
                        wx.showToast({
                            title: '收藏成功！',
                        })
                    }
                    else {
                        this.setData({
                            heart: true
                        })
                        wx.showToast({
                            title: '取消收藏！',
                        })
                    } 
                }
            }
        })
           
    },
    handleReferShow() {
        this.setData({
            referShow: true
        })
    },
    handleReferHidden() {
        this.setData({
            referShow: false
        })
    },
    handleSecondShow(e) {
        this.setData({
            second: true,
            bottom: -300,
            scrollTop: 0
        })
        let self = this
        let timer = setInterval(() => {
            if(self.data.bottom < 100) {
                self.setData({
                    bottom: self.data.bottom + 10
                })
            }
            else {
                clearInterval(timer)
            }
        },10)
        
    },
    handleSecondHidden() {
        this.setData({
            bottom: 100
        })
        let self = this
        let timer = setInterval(() => {
            if (self.data.bottom > -300) {
                self.setData({
                    bottom: self.data.bottom - 10
                })
            }
            else {
                this.setData({
                    second: false
                })
                clearInterval(timer)
            }
        }, 10)
    },
    handleScroll(e) {
        this.setData({
            scrollTop: e.detail.scrollTop
        })
    }
})